/*
 * Created on 08.06.2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package ua.org.sovic.Common;

import java.io.File;

/**
 * @author sovic
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Config extends Settings {
    protected static String project;    // Name this project 
    protected static String fileName;   // FileName config project 
    protected static String propDir;    // Directory to save project config
    public static Config SET;

    static {
        SET = new Config();
    }
    /** Creates a new instance of ConfMain */
    public Config() {
        this((String) System.getProperty("Project"), (String) System.getProperty("ProjectConfig"));
    }
    @SuppressWarnings({"static-access", "LeakingThisInConstructor"})
    public Config(String _project, String _fileName) {
        super();
        this.project = _project;
        this.fileName = _fileName;
        this.propDir = System.getProperty("user.home") + 
                       System.getProperty("file.separator") +
                       "." + _project;
        setProjectName(_project);
        SET = this;
    }
    
    @SuppressWarnings("CallToThreadDumpStack")
    public static boolean findSettings(String fileName) {
    	try {
    		File file = new File(propDir, fileName);
    		// Check to reality path working directory
    		File dir = new File(propDir);
                System.out.println("Work dir: " + propDir);
    		if (!dir.exists()) dir.mkdirs();
    		if (file.exists()) return true;
    	} catch (Exception exc) {
                System.out.println("Error create work directory...");
                exc.printStackTrace();
    		System.exit(1);
    	}
    	return false;
    }
    @SuppressWarnings("CallToThreadDumpStack")
    public static boolean load() {
        try {
            if (findSettings(fileName)) {
                SET.clearHash();
                SET.load(new File(propDir, fileName));
                System.out.println("Loading settings...");
                return true;
            } else {
                return false;
            }
        } catch (Exception exc) {
            exc.printStackTrace();
        }
        return false;
    }
    
    @SuppressWarnings("CallToThreadDumpStack")
    public static void save() {
        try {
            SET.save(new File(propDir, fileName));        
        } catch (Exception exc) {
            exc.printStackTrace();
        }
    }
    public static void clear() {
        SET.clearHash();
    }
    public static boolean isLoading() {
        return SET.getLoading();
    }
    public static void setProject(String project) {
        SET.setProjectName(project);
    }
    
    public static Object get(String key) {
        return SET.getKey(key);
    }
    public static Object get(String key, Object deflt) {
        return SET.getKey(key);
    }
    public static String getString(String key) {
        return (String) SET.getKey(key);
    }
    public static String getString(String key, String deflt) {
        return (String) SET.getKey(key, deflt);
    }
    public static void put(String key, Object data) {
        //prevent null values. Hasmap allow them
        if (data == null) {
            throw new IllegalArgumentException();
        } else {
            SET.putKey(key, data);
        }
    }    
}